package com.heima.article.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.admin.AuthorControllerApi;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.article.service.AuthorService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 王可
 * @Date: 2021/01/25/23:27
 * @Description:
 */
@RestController
@RequestMapping("/api/v1/author")
public class AuthorController implements AuthorControllerApi {

    @Resource
    private AuthorService authorService;

    @GetMapping("/findByUserId/{id}")
    @Override
    public ApAuthor findByUserId(@PathVariable("id") Integer id) {
        List<ApAuthor> list = authorService.list(Wrappers.<ApAuthor>lambdaQuery()
                .eq(ApAuthor::getId, id));
        if(list != null && !list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    @PostMapping("/save")
    @Override
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        if(apAuthor != null){
            authorService.save(apAuthor);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return null;
    }
}
